<template>
  <div class="about" style="margin-left:50px;margin-top:50px;box-shadow: 5px 5px 20px #d5cece;padding: 40px;width: 90%;background:#fff">
    <Tree :isSelect="isSelect" :data="list" @node-click="handle" @change="loadData" v-loading="listLoading" />

 <div style="
  font-weight: 600;
  font-size: 20px;
  color: #6e6d6d;
  display: inline-block;

  float: right;">当前账户：
<span style="font-size:38px;
  letter-spacing: 5px;
  color: #f9994f;">{{title}}</span>
      
    </div>
  </div>
</template>

<script>
import Tree from '@/Tree.vue'
import json from '@/mock.js'
import { getManagerApi } from "@/api/user";
import { getNickName,getRoleCode} from "@/utils/auth";

export default {
  components: { Tree },
  data() {
    return {
      treeData: [],
      isSelect: false,
      defaultProps: {
        children: 'children',
        label: 'loginName',
        key: 'id',
      },
      title:'',
      list:[],
      listLoading:true
    }
  },

  created() {   
      var _this = this;
      _this.title = "";
      // localStorage.removeItem("key");
      // localStorage.removeItem("title")
      _this.getManager();

      setTimeout(() => {
          _this.listLoading = false;
          }, 1 * 2000);
  },
  mounted(){
    var _this = this;
    var key = localStorage.getItem("key");
      if(key == '' || key == undefined || key == null){
      this.roleCode = getRoleCode()
      localStorage.setItem("key",this.roleCode)
     
      }else{
        this.roleCode = key
      }
      
     var userName = localStorage.getItem("title");
      if(userName == '' || userName == undefined || userName == null){
        this.userName = getNickName();
        localStorage.setItem("title",this.userName)
      }else{
        this.userName = localStorage.getItem("title");
      }
     
  },
  methods: {
    handle(node) {
      localStorage.setItem("key",node.roleCode)
      localStorage.setItem("title",node.title)
      this.title = localStorage.getItem("title")
      console.log('点击节点 Data : ', node)
      // this.$store.commit('updatetitle',node.title)
      // this.$store.commit('updatekey',node.key)
      this.$router.push({path:'/table/parkfield'})
     },
     getManager(){
         getManagerApi().then((response) => {
         var list=[] 
         var listtemp=[] 
         var list8=[] 
         var len = response.data.length;
         for(var i=0;i < len;i++){
           const list1={};
           list1["key"]=response.data[i].id;
           list1["title"]=response.data[i].nickName;
           list1["roleCode"]=response.data[i].roleCode;
           list1["children"]=[];
           listtemp.push(list1)
         }


         for(var i=0;i < len;i++){
          if(listtemp[i].roleCode.length == 4){
              list.push(listtemp[i])
          }
          }

          if(list == ""){
            for(var i=0;i < len;i++){
              if(listtemp[i].roleCode.length == 8){
                listtemp[i].roleCode =  listtemp[i].roleCode.substring(0,4)
                  list.push(listtemp[i])

              }
            }

            // console.log("3333333",list)
          }

          for(var i=0;i < len;i++){
          if(listtemp[i].roleCode.length == 8){
              list8.push(listtemp[i])
          }
          }

          for(var i=0;i < len;i++){
          if(listtemp[i].roleCode.length == 12){    
              for(var j=0;j < list8.length;j++){
              if(listtemp[i].roleCode.substring(0,8) == list8[j].roleCode){
                  list8[j].children.push(listtemp[i])
              }
          }
          }
          }

          list[0].children = list8
          this.list = list;


          


          // console.log("151515151515151515",list[0].roleCode)

          var keyNull = localStorage.getItem("key");
          var titleNull = localStorage.getItem("title");
          if(keyNull == '' || keyNull == null || titleNull == '' || titleNull == null){
            localStorage.setItem("key", list[0].roleCode)
            localStorage.setItem("title", list[0].title)
            this.title = list[0].title
          }else{
            this.title = localStorage.getItem("title")
          }

          // var keyNull = this.$store.state.mainpart.key
          // if(keyNull == '' || keyNull == null || keyNull == undefined){
          //   this.$store.commit('updatekey',list[0].key)
          //   console.log("99999999999999999999",this.$store.state.mainpart.key)
          // }else{
            
          // }

          });
     },
    
    
    loadData(treeNode) {
      console.log(treeNode)
    }
  }
 
}
</script>